﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using VOL;
using DAL;

namespace BLL
{
    public class NguoiQuanLyBL
    {
        private DataAccess dataAccess;
        public NguoiQuanLyBL()
        {
            dataAccess = new DataAccess();
        }

        public DataTable GetData()
        {
            string strSql = "SELECT * FROM tbl_NguoiQuanLy ORDER BY MaNguoiQuanLy DESC ";
            return dataAccess.GetData(strSql);
        }

        //Thêm
        public bool insertNguoiQuanLy(NguoiQuanLy obj)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@MatKhau", SqlDbType.NVarChar) { Value = obj.MatKhau };
            param[1] = new SqlParameter("@TenDangNhap", SqlDbType.NVarChar) { Value = obj.TenDangNhap };
            param[2] = new SqlParameter("@HoTen", SqlDbType.NVarChar) { Value = obj.HoTen };
            param[3] = new SqlParameter("@Email", SqlDbType.NVarChar) { Value = obj.Email };

            string strSql = "INSERT INTO tbl_NguoiQuanLy(TenDangNhap,MatKhau,HoTen,Email) VALUES (@TenDangNhap,@MatKhau,@HoTen,@Email)";
            dataAccess.Execute(strSql, param);

            return true;
        }

        ////Xóa
        public bool deleteNguoiQuanLy(NguoiQuanLy obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaNguoiQuanLy", SqlDbType.Int) { Value = obj.MaNguoiQuanLy };

            string strSql = "DELETE FROM tbl_NguoiQuanLy WHERE MaNguoiQuanLy = @MaNguoiQuanLy";
            dataAccess.Execute(strSql, param);
            return true;
        }

        ////Sửa
        public bool updateNguoiQuanLy(NguoiQuanLy obj)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@MatKhau", SqlDbType.NVarChar) { Value = obj.MatKhau };
            param[1] = new SqlParameter("@TenDangNhap", SqlDbType.NVarChar) { Value = obj.TenDangNhap };
            param[2] = new SqlParameter("@HoTen", SqlDbType.NVarChar) { Value = obj.HoTen };
            param[3] = new SqlParameter("@Email", SqlDbType.NVarChar) { Value = obj.Email };
            param[4] = new SqlParameter("@MaNguoiQuanLy", SqlDbType.Int) { Value = obj.MaNguoiQuanLy };


            string strSql = "UPDATE tbl_NguoiQuanLy SET " +
                            "TenDangNhap = @TenDangNhap, " +
                            "HoTen = @HoTen, " +
                            "MatKhau = @MatKhau, " +
                            "Email = @Email " +
                            "WHERE MaNguoiQuanLy = @MaNguoiQuanLy";
            dataAccess.Execute(strSql, param);
            return true;
        }

        ////Tìm theo Mã Thành Phố
        public DataTable searchIDNguoiQuanLy(int maNguoiQuanLy)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaNguoiQuanLy", SqlDbType.Int) { Value = maNguoiQuanLy };

            string strSql = "SELECT * FROM tbl_NguoiQuanLy WHERE MaNguoiQuanLy = @MaNguoiQuanLy";
            return dataAccess.GetData(strSql, param);
        }

        public NguoiQuanLy Login(string tenTruyCap, string matKhau)
        {
            NguoiQuanLy obj = new NguoiQuanLy();

            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@TenDangNhap", SqlDbType.NVarChar) { Value = tenTruyCap };
            param[1] = new SqlParameter("@MatKhau", SqlDbType.NVarChar) { Value = matKhau };
            // cau query select
            string sql = "SELECT MaNguoiQuanLy, HoTen FROM tbl_NguoiQuanLy WHERE TenDangNhap = @TenDangNhap AND MatKhau = @MatKhau";
            DataTable dt = dataAccess.GetData(sql, param);
            if (dt.Rows.Count > 0)
            {
                obj.TenDangNhap = tenTruyCap;
                obj.MatKhau = matKhau;
                obj.MaNguoiQuanLy = int.Parse(dt.Rows[0]["MaNguoiQuanLy"].ToString());
                obj.HoTen = dt.Rows[0]["HoTen"].ToString();

                return obj;
            }

            return null;
        }
    }
}
